<?php
namespace Api\Controller;
use Think\Controller;
class AddressController extends CommonController {
	public function _initialize(){
		
	}
	//地址添加接口
    public function insert(){
        $id 		= I('get.id');
        $name		= I('get.name');
		$telephone	= I('get.telephone');
		$province	= I('get.province');
		$city		= I('get.city');
		$area		= I('get.area');
		$intro		= I('get.intro');
		
		
        $address = M('Address');
        
        $res = $address->where("user_id={$id}")->find();
		$list=$address->create();
		
		$data =array(
			'name'		=>	$name,
			'telephone'	=>	$telephone,
			'province'	=>	$province,
			'city'		=>	$city,
			'area'		=>	$area,
			'intro'		=>	$intro,
			'user_id'	=>	$id,
			'addtime'	=>	time()
		);
		$list = $address->add($data);
		
		
		$this->response( true , 0 , '添加成功' , true);
        
    }
    //地址修改接口
    public function edit(){
		$id 		= I('get.id');
        $name		= I('get.name');
		$telephone	= I('get.telephone');
		$province	= I('get.province');
		$city		= I('get.city');
		$area		= I('get.area');
		$intro		= I('get.intro');
		
		$address = M('Address');
		
		if( $id != ''){
			$list = $address->where("id={$id}")->find();
			
			if( $list == false){
				$this->response( false , 1 , '没有数据' , true);
			}
			else{
				$data = array(
					'name'		=>	$name,
					'telephone'	=>	$telephone,
					'province'	=>	$province,
					'city'		=>	$city,
					'area'		=>	$area,
					'intro'		=>	$intro,
					'addtime'	=>	time()
				);
				$list = $address->where("id={$id}")->save($data);
			}
			$this->response( true , 0 , '修改成功' , true);
		}
	}
	
	//设置默认地址
	public function defaddress(){
		//获取点击地址的id
		$click	= I('get.click');
		$id		= $_SESSION['uid'];

		$address = M('Address');
		
		if( $click != ''){
			$data = array(
				'click'	=> 1
			);
			$num = array(
				'click'	=> 0
			);
			$list = $address->where("id={$click}")->save($data);//点击的设置1
			$list = $address->where("id!={$click} and user_id={$id}")->save($num);//未点击的设置0
			
			$this->response( true , 0 , '默认地址设置成功' , true);
		}
		else{
			$this->response( false , 1 , '默认地址设置失败' , true);
		}
		
	}
	
	
}
